package com.ss.android.image.a.a;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;

import com.facebook.common.internal.Preconditions;
import com.facebook.datasource.AbstractDataSource;
import com.facebook.drawee.controller.AbstractDraweeController;
import com.facebook.drawee.controller.AbstractDraweeControllerBuilder;
import com.facebook.drawee.interfaces.SimpleDraweeControllerBuilder;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Request;

public final class TTDraweeControllerBuilder extends AbstractDraweeControllerBuilder<TTDraweeControllerBuilder, Request, Drawable, Drawable> {
    private final TTDraweeControllerFactory mTTDraweeControllerFactory;

    protected TTDraweeControllerBuilder(Context context, TTDraweeControllerFactory ttDraweeControllerFactory) {
        super(context);
        this.mTTDraweeControllerFactory = ttDraweeControllerFactory;
    }

    public final SimpleDraweeControllerBuilder setUri(Uri uri) {
        return this.setImageRequest(Picasso.with(this.mContext).load(uri).createRequest(System.nanoTime()));
    }

    public final AbstractDataSource getDataSourceForRequest(Request request, boolean justCache) {
        return new PicassoImage(request, justCache);
    }

    public final SimpleDraweeControllerBuilder setUri(String uri) {
        Preconditions.checkNotNull(uri);
        return this.setImageRequest(Picasso
                .with(this.mContext)
                .load(Uri.parse(uri))
                .createRequest(System.nanoTime()));
    }

    public final AbstractDraweeController obtainController() {
        if ((mOldController instanceof TTDraweeController)) {
            ((TTDraweeController) mOldController).initialize(this.obtainDataSourceSupplier(), generateUniqueControllerId(), this.mCallerContext);
        } else {
            mOldController = new TTDraweeController(
                    this.mTTDraweeControllerFactory.mDeferredReleaser,
                    this.mTTDraweeControllerFactory.mUiThreadExecutor,
                    this.obtainDataSourceSupplier(),
                    generateUniqueControllerId(),
                    this.mCallerContext);
        }

        return ((AbstractDraweeController) mOldController);
    }

    public final TTDraweeControllerBuilder getThis() {
        return this;
    }
}

